package com.up.vms.application.service;

import com.alibaba.fastjson.JSON;
import com.up.vms.application.common.DomainUtils;
import com.up.vms.domain.module.*;
import com.up.vms.domain.op.entity.UserDO;
import com.up.vms.domain.vm.entity.Activity;
import com.up.vms.domain.vm.entity.ActivityDetail;
import com.up.vms.domain.vm.entity.UserActivityInfo;
import com.up.vms.domain.vm.enums.ActivityStatus;
import com.up.vms.domain.vm.service.ActivityDetailService;
import com.up.vms.domain.vm.service.ActivityService;
import com.up.vms.domain.vm.service.UserActivityInfoService;
import com.up.vms.infrastructure.mvc.GeneRespDTO;
import com.up.vms.infrastructure.mvc.RespCode;
import com.up.vms.infrastructure.mvc.ReturnCode;
import com.up.vms.infrastructure.util.CommonUtils;
import com.up.vms.infrastructure.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ActivityApplicationService {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityDetailService activityDetailService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserActivityInfoService userActivityInfoService;

    public GeneRespDTO getActivityList(ActivityDTO activityDTO) throws Exception {
        GeneRespDTO geneRespDTO = null;

        Page<Activity> simpleShortVideos = null;
        simpleShortVideos = activityService.findAllByCase(activityDTO);

        if (CommonUtils.isNotNull(simpleShortVideos)) {
            geneRespDTO = DomainUtils.convert(simpleShortVideos);
        } else {
            throw new Exception("没有记录");
        }
        return geneRespDTO;
    }

    public GeneRespDTO saveActivity(Activity activity) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();
        Activity saveActivity = null;
        activity.setActivityTime(DateUtil.format(activity.getStartDate(), "yyyy.M.d") + " - " +
                DateUtil.format(activity.getEndDate(), "yyyy.M.d")
                );
        if (CommonUtils.isNotNull(activity.getId())) {
            saveActivity = activityService.getById(activity.getId());
        }
        if (CommonUtils.isNotNull(saveActivity)) {
            saveActivity.setActivityName(activity.getActivityName());
            saveActivity.setActivityDesc(activity.getActivityDesc());
            saveActivity.setActivityImage(activity.getActivityImage());
            saveActivity.setStartDate(activity.getStartDate());
            saveActivity.setEndDate(activity.getEndDate());
            saveActivity.setActivityTime(activity.getActivityTime());
            saveActivity.setAdditionalInfo(activity.getAdditionalInfo());
            saveActivity.setModified(new Date());
        } else {
            Activity activity1 = activityService.getByName(activity.getActivityName());
            if (activity1 != null) {

                geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
                geneRespDTO.setData(activity1);

                return geneRespDTO;
            }
            saveActivity = activity;
            saveActivity.setId(null);
            saveActivity.setCreated(new Date());
        }
        if (DateUtil.compareDate(new Date(), activity.getEndDate()) > 0) {
            saveActivity.setStatus(ActivityStatus.CREATED.code);
        } else {
            saveActivity.setStatus(ActivityStatus.FINISHED.code);
        }

        Activity saved = activityService.save(saveActivity);

        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
        geneRespDTO.setData(saved);

        return geneRespDTO;
    }

    public GeneRespDTO deleteActivity(List<Long> idList) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();

        if (CommonUtils.isNotNull(idList)) {
            for (Long id : idList) {
                Activity activity = activityService.getById(id);
                activity.setStatus(ActivityStatus.DELETED.code);
                activityService.save(activity);
            }
        }

        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
        geneRespDTO.setData(idList);
        return geneRespDTO;
    }

    public GeneRespDTO uploadDetail(ActivityDetailVO activityDetailVO, Long userId) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();

        UserDO userDO = userService.getById(userId);

        boolean isChecked = false;
        if (CommonUtils.isNotNull(userDO.getIdNumber())) {
            Long count = userActivityInfoService.countByIdNumberAndActivityId(userDO.getIdNumber(), activityDetailVO.getActivityId());
            if (count > 0) {
                isChecked = true;
            } else {
                count = userActivityInfoService.countByIdNumberAndActivityId(userDO.getIdNumber(), 0L);
                if (count > 0) {
                    isChecked = true;
                }
            }
        }

        if (!isChecked) {
            geneRespDTO.setRespCode(new RespCode("EDI-30401", "用户不在授权列表中"));
            return geneRespDTO;
        }

        ActivityDetail activityDetail = activityDetailService.findByActivityIdAndUserId(activityDetailVO.getActivityId(), userId);

        if (CommonUtils.isNotNull(activityDetail)) {
            activityDetail.setModified(new Date());
        } else {
            activityDetail = new ActivityDetail();
            activityDetail.setActivityId(activityDetailVO.getActivityId());
            activityDetail.setUserId(userId);
            activityDetail.setCreated(new Date());
        }
        activityDetail.setUploadTime(new Date());

        if (CommonUtils.isNotNull(activityDetailVO.getAdditionalInfoList())) {
            activityDetail.setAdditionalInfo(JSON.toJSONString(activityDetailVO.getAdditionalInfoList()));
        } else {
            activityDetail.setAdditionalInfo(null);
        }
        if (CommonUtils.isNotNull(activityDetailVO.getImageList())) {
            activityDetail.setUploadImage(JSON.toJSONString(activityDetailVO.getImageList()));
        } else {
            activityDetail.setUploadImage(null);
        }

        activityDetail.setStatus(ActivityStatus.DEFAULT.code);

        activityDetailService.save(activityDetail);

        geneRespDTO = getActivityInfo(activityDetailVO.getActivityId(), userId);

        return geneRespDTO;
    }

    public GeneRespDTO getActivityInfo(Long activityId, Long userId) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();
        ActivityVO activityVO = new ActivityVO();
        UserDO userDO = userService.getById(userId);
        Activity activity = activityService.getById(activityId);
        activityVO.setId(activity.getId());
        activityVO.setActivityName(activity.getActivityName());
        activityVO.setActivityDesc(activity.getActivityDesc());
        activityVO.setActivityImage(activity.getActivityImage());
        activityVO.setStartDate(activity.getStartDate());
        activityVO.setEndDate(activity.getEndDate());
        if (CommonUtils.isNotNull(activity)) {
            activityVO.setAdditionalInfo(JSON.parseArray(activity.getAdditionalInfo(), ActivityAdditionalInfoVO.class));
        }
        ActivityDetail activityDetail = activityDetailService.findByActivityIdAndUserId(activityId, userId);
        ActivityDetailVO detailVO;
        if (activityDetail != null) {

            detailVO = new ActivityDetailVO(
                    activityDetail.getId(), activityDetail.getUploadTime(),
                    userDO.getUserName(), userDO.getUserPhone(), userDO.getIdNumber());
            if (CommonUtils.isNotNull(activityDetail.getAdditionalInfo())) {
                detailVO.setAdditionalInfoList(JSON.parseArray(activityDetail.getAdditionalInfo(), ActivityUploadAdditionalInfoVO.class));
            }
            if (CommonUtils.isNotNull(activityDetail.getUploadImage())) {
                detailVO.setImageList(JSON.parseArray(activityDetail.getUploadImage(), ActivityUploadImageVO.class));
            }
        } else {
            detailVO = new ActivityDetailVO(
                    null, null,
                    userDO.getUserName(), userDO.getUserPhone(), userDO.getIdNumber());
        }

        activityVO.setDetail(detailVO);

        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
        geneRespDTO.setData(activityVO);

        return geneRespDTO;
    }

    public GeneRespDTO getActivityDetailList(ActivityDetailDTO detailDTO) throws Exception {

        GeneRespDTO geneRespDTO = null;

        Page<ActivityDetailVO> simpleShortVideos = null;
        simpleShortVideos = activityDetailService.findAllByCaseDetail(detailDTO);

        if (CommonUtils.isNotNull(simpleShortVideos)) {
            geneRespDTO = DomainUtils.convert(simpleShortVideos);
        } else {
            throw new Exception("没有记录");
        }
        return geneRespDTO;
    }

    public GeneRespDTO queryActivityById(Long id) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();
        Activity activity = activityService.getById(id);
        if (activity != null) {
            geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
            geneRespDTO.setData(activity);
        } else {
            geneRespDTO.setRespCode(new RespCode(ReturnCode.SERVICE_ERROR.getCode(), "未查询到记录信息"));
        }
        return geneRespDTO;
    }

    public GeneRespDTO queryActivityDetailById(Long detailId) {

        GeneRespDTO geneRespDTO = new GeneRespDTO();
        ActivityDetail activityDetail = activityDetailService.getById(detailId);
        if (activityDetail != null) {
            UserDO userDO = userService.getById(activityDetail.getUserId());
            ActivityDetailVO detailVO = new ActivityDetailVO(
                    activityDetail.getId(), activityDetail.getUploadTime(),
                    userDO.getUserName(), userDO.getUserPhone(), userDO.getIdNumber());
            if (CommonUtils.isNotNull(activityDetail.getAdditionalInfo())) {
                detailVO.setAdditionalInfoList(JSON.parseArray(activityDetail.getAdditionalInfo(), ActivityUploadAdditionalInfoVO.class));
            }
            if (CommonUtils.isNotNull(activityDetail.getUploadImage())) {
                detailVO.setImageList(JSON.parseArray(activityDetail.getUploadImage(), ActivityUploadImageVO.class));
            }

            geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
            geneRespDTO.setData(detailVO);
        } else {
            geneRespDTO.setRespCode(new RespCode(ReturnCode.SERVICE_ERROR.getCode(), "未查询到记录信息"));
        }
        return geneRespDTO;
    }

    public GeneRespDTO getUserInfo(Long userId) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();
        UserDO userDO = userService.getById(userId);
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setUserName(userDO.getUserName());
        userInfoVO.setUserPhone(userDO.getUserPhone());
        userInfoVO.setIdNumber(userDO.getIdNumber());

        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
        geneRespDTO.setData(userInfoVO);

        return geneRespDTO;
    }

    public GeneRespDTO saveUsers(List<UserActivityInfo> users, Long activityId, boolean isReset) {

        GeneRespDTO geneRespDTO = new GeneRespDTO();

        if (isReset) {
            userActivityInfoService.deleteByActivityId(activityId);
        }

        for(UserActivityInfo user : users) {
            UserActivityInfo userActivityInfo = userActivityInfoService.findByIdNumberAndActivityId(user.getIdNumber(), activityId);
            if (CommonUtils.isNotNull(userActivityInfo)) {
                user.setId(userActivityInfo.getId());
            }
            userActivityInfoService.save(user);
        }

        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));

        return geneRespDTO;
    }

    public GeneRespDTO deleteActivityUser(Long id) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();

        if (CommonUtils.isNotNull(id)) {
            ActivityDetail detail = activityDetailService.getById(id);
            if (CommonUtils.isNotNull(detail)) {
                detail.setStatus(ActivityStatus.DELETED.code);
                activityDetailService.save(detail);
            }
        }

        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
        geneRespDTO.setData(id);
        return geneRespDTO;
    }
}
